package com.makefuture.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.makefuture.model.TUser;
import com.makefuture.model.TUserrole;

/**
 * HttpServletResponse response = this.getResponse();
			HttpSession session = this.getSession();
			session.getAttribute("")
 *
 * @param <Record>
 */

public class UserController<Record> extends BaseController {

	 public UserController() {  
		   setClazz(TUser.class);  
		  }  
	
	public void index() {
		String sql = " select * from t_user ";
		setAttr("userlist",  Db.find(sql));
	  //  setSessionAttr("tuser", "dudu"); 
	 //   System.out.println(getSessionAttr("tuser"));
		Page<Record> pageGallery = (Page<Record>) Db.paginate(1, 10, " select *  ","", " from (select u.id uid,u.name,b.id from t_user u left join T_ProblemSummary b on u.id = b.userid ) t  ");
	//	Page<Record> pageGallery = (Page<Record>) Db.paginate(1, 10, " select *  ","", " from (select u.id uid,u.name,b.id from t_user u left join T_ProblemSummary b on u.id = b.userid ) t where t.id > ?  ",10);
//		setAttr("pageGallery",pageGallery);//页面 ${pageGallery.list}//获取查询的list数据   其余参看Page对象属性
	//	Page<Record>  pageGallery=(Page<Record>) TUser.dao.paginate(1,20, " select * ", " from t_user u left join T_ProblemSummary b on u.id = b.userid  ");
	//	List<Record> list = pageGallery.getList();
	//	System.out.println(list.toString());
		//System.out.println(TUser.dao.find(gallerys.toString()));
//	   setAttr("userlist", list);
//	   Map<String, Object> map = new HashMap<String, Object>();
//	   map.put("pageNum", pageGallery.getPageNumber());
//	   map.put("toalPage", pageGallery.getTotalPage());
//	   map.put("toalRow", pageGallery.getTotalRow());
//	   map.put("pageSize", pageGallery.getPageSize());
//	   setAttr("maps",map);
//	   Page<Record>   pageRec=(Page<Record>) TUserrole.dao.paginate(
//               1,
//               10,
//               " select * ",
//               " from (" +
//               "  select  t1.no,t1.name,t2.sexName,t1.age,t3.gradeName  from student     t1 " +
//               "    left join  sex   t2   on t1.sexId=t2.id " +
//               "    left join  grade t3   on t1.gradeId=t3.id )" +
//               " tt where 1=1");
		render("list.jsp");
	}

	public void toAdd() {
		String sql = " select* from T_UserRole ";
		setAttr("roleList", TUserrole.dao.find(sql));
	//	setSessionAttr("tuser",TUserrole.dao.find(sql)); 
		// 删除session中的属性  
		removeSessionAttr("loginUser");  
		render("add.jsp");
	}

	public void add() {
		TUser user = this.getModel(TUser.class);
		
		System.out.println(user.getId());
		if (user.getId() != null &&!user.getId().equals("")) {
			
			user.update();
			System.out.println("id:" + user.get("id"));
			// forwardAction("/user");
		} else {
			user.removeNullValueAttrs();
			user.save();
            System.out.println("id:" + user.get("id"));
		}
		redirect("/user");//重定向 controller跳转自己的方法 不需要加路径 直接写方法就行
	}

	public void toUpdate() {
		String id = getPara("id");
		Map<String, Object> map = new HashMap<String, Object>();
		setAttr("user", TUser.dao.findById(id));
		String sqls = " select roleId,roleName from T_UserRole ";
		setAttr("roleList", TUserrole.dao.find(sqls));
		HttpSession session = this.getSession();
		session.setAttribute("role", TUser.dao.findById(id));
		TUser ro = (TUser) session.getAttribute("role");
		System.out.println("session ro:"+ro.toString());
		System.out.println(id);
		render("add.jsp");
	}
	
//	@Before(Tx.class)
	public void delete() {
		String id = getPara("id");
		System.out.println(id);
		if (id != null &&! id.equals("")) {
			TUser user = new TUser();
			boolean flag = user.deleteById(id);
			System.out.println(flag);
			
			System.out.println(user.getId());
			System.out.println(user.toString());
		//	int t = Integer.parseInt("abd");
			renderJson("msg", "ok");
		}else {
			renderJson("msg", "error");
		}
	}
	
	

}
